const Mock = require('mockjs')

const delay = (ms = 3000) => {
  return new Promise((_) => {
    setTimeout(() => {
      _('')
    }, ms)
  })
}

module.exports = (app) => {
  // 添加角色
  app.post('/api/role/create', (req, res) => {
    res.send({
      code: 0,
      msg: 'ok',
      data: Date.now()
    })
  })
  // 角色列表
  app.get('/api/role/list', (req, res) => {
    res.send({
      code: 0,
      msg: 'ok',
      data: {
        total: 20,
        roles: [
          { id: 1, name: '管理员' },
          { id: 2, name: '财务' },
          { id: 3, name: '人事' },
          { id: 4, name: '业务总监' }
        ]
      }
    })
  })
  // 根据角色id返回对应它的用户列表
  app.get('/api/role/roleidToUsers/:id', async (req, res) => {
    await delay()
    let page = req.query.page ? Number(req.query.page) : 1
    let size = req.query.size ? Number(req.query.size) : 10
    const users = Mock.mock({
      [`users|${size}`]: [
        {
          'id|+1': page * size,
          username: '@name',
          realname: '@cname',
          'age|18-100': 18,
          'sex|1': [1, 2],
          birthday: '@date',
          avatar: 'http://video.1314000.cn/face.png',
          city: ['北京', '昌平'],
          address: '北京昌平地区沙河大桥'
        }
      ]
    })
    res.send({
      code: 0,
      msg: 'ok',
      data: {
        ...users,
        total: 100
      }
    })
  })

  // 根据角色id返回它已有的权限 -- 有菜单
  app.get('/api/role/:id/menuauths', (req, res) => {
    res.send({
      code: 0,
      msg: 'ok',
      data: {
        menus: [
          {
            id: 1,
            label: '信息汇总',
            ename: 'info',
            pid: 0,
            menu: 1,
            route: '/info',
            icon: 'TeamOutlined',
            children: [
              {
                id: 10,
                label: '图形汇总',
                ename: 'dashoard',
                pid: 1,
                menu: 1,
                route: '/info/dashoard',
                icon: 'TeamOutlined'
              }
            ]
          },
          {
            id: 2,
            label: '后台设置',
            ename: 'sys',
            pid: 0,
            menu: 1,
            route: '/sys',
            icon: 'TeamOutlined',
            children: [
              {
                id: 20,
                label: '用户列表',
                ename: 'user',
                pid: 2,
                menu: 1,
                route: '/sys/user',
                icon: 'TeamOutlined'
              },
              {
                id: 22,
                label: '角色列表',
                ename: 'role',
                pid: 2,
                menu: 1,
                route: '/sys/role',
                icon: 'TeamOutlined'
              },
              {
                id: 23,
                label: '节点列表',
                ename: 'authnode',
                pid: 2,
                menu: 1,
                route: '/sys/auth',
                icon: 'TeamOutlined'
              }
            ]
          },
          {
            id: 3,
            label: '电影管理',
            ename: 'film',
            pid: 0,
            menu: 1,
            route: '/film',
            icon: 'TeamOutlined',
            children: [
              {
                id: 31,
                label: '电影列表',
                ename: 'film-list',
                pid: 0,
                menu: 3,
                route: '/film/list',
                icon: 'TeamOutlined'
              },
              {
                id: 32,
                label: '新增电影',
                ename: 'film-add',
                pid: 0,
                menu: 3,
                route: '/film/add',
                icon: 'TeamOutlined'
              }
            ]
          }
        ],
        btns: ['adduser']
      }
    })
  })
}
